/*
 * @Author: “ljk” “2396923396@qq.com”
 * @Date: 2023-12-14 09:29:23
 * @LastEditors: “ljk” “2396923396@qq.com”
 * @LastEditTime: 2023-12-14 09:46:55
 * @FilePath: \components\src\utils\publicUplod.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
 const preViewOne=function(fieldId) {
  // return file.download(fieldId).then(res => {
  //   var blob = new Blob([res],
  //     {
  //       type: 'application/octet-stream;charset=UTF-8'
  //     });
  //   return window.URL.createObjectURL(blob);
  // })
  return file.preview(fieldId).then(response => {
    window.open(response.data)
  })
}
 const preView=function(fieldId) {
    return file.download(fieldId).then(res => {
      var blob = new Blob([res],
        {
          type: 'application/octet-stream;charset=UTF-8'
        });
      window.URL.createObjectURL(blob);
      return new Promise(function (resolve,reject) {
        resolve(res)
      })
    })
    .catch(res=>{
      console.log(res)
      // Vue.prototype.$messageError('请求失败!')
    })
  }
// 文件下载
const checkFile=function(fieldId, fileName) {
  return file.download(fieldId).then(res => {
    var blob = new Blob([res],
      {
        type: 'application/octet-stream;charset=UTF-8'
      });
    var downloadElement = document.createElement('a');
    var href = window.URL.createObjectURL(blob); // 创建下载的链接
    downloadElement.style.display = 'none';
    downloadElement.href = href;
    downloadElement.download = decodeURI(fileName); // 下载后文件名
    document.body.appendChild(downloadElement);
    downloadElement.click(); // 点击下载
    document.body.removeChild(downloadElement); // 下载完成移除元素
    window.URL.revokeObjectURL(href); // 释放掉blob对象
    return new Promise(resolve => {
      resolve(res)
    })
  })
}
const previewFile = function(row) {
  if (!row.id) {
    Toast('该文件不支持预览')
    return
  }
  let imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
  let videolist = ['mp4', 'm2v', 'mkv', 'mov', 'avi']
  let pdflist = ['pdf']
  if (!row.name && row.fileName) {
    row.name = row.fileName
  }
  if (imglist.includes(row.name.split('.')[row.name.split('.').length - 1].toLowerCase())) {
    Vue.prototype.$ContinueToast('正在加载中...')
    request.get('/file-server/file/preview?id=' + row.id).then(response => {
      Vue.prototype.$ClearTips()
      ImagePreview([response.data])
    })
  } else if ((videolist.includes(row.name.split('.')[row.name.split('.').length - 1].toLowerCase()))) {
    console.log('视频类型')
  } else if ((pdflist.includes(row.name.split('.')[row.name.split('.').length - 1].toLowerCase()))) {
    Vue.prototype.$ContinueToast('正在加载中...')
    request.get('/file-server/file/preview?id=' + row.id).then(response => {
      Vue.prototype.$ClearTips()
      console.log(response.data.replace('/file-server', ''), 'pdf')
      // 本地 测试环境 需要 删除file-server
      // goToPdf(response.data.replace('/api/file-server', ''))
      goToPdf(response.data)
      Vue.prototype.$ClearTips()
    })
  } else {
    Vue.prototype.$ContinueToast('正在加载中...')
    request.get('/file-server/file/preview?id=' + row.id).then(response => {
      console.log(response.data.replace('/file-server', ''), '非pdf')
      // 本地 测试环境 需要 删除file-server
      // 预生产 生产环境 需要 保留file-server
      // goToPdf(response.data.replace('/api/file-server', ''))
      goToPdf(response.data)
      Vue.prototype.$ClearTips()
    })
  }
}
